package com.easy.service.impl;

import com.easy.bean.Order;
import com.easy.dao.IOrderDao;
import com.easy.dao.LogisticsOrderMapper;
import com.easy.service.IOrderService;
import com.easy.util.LogisticsOrderStatistics;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    IOrderDao dao;

    @Autowired
    LogisticsOrderMapper logisticsOrderMapper;

    @Override
    public int addOrder(Order order) {
        return dao.addOrder(order);
    }

    @Override
    public int delOrder(int id) {
        return dao.delOrder(id);
    }

    @Override
    public int editOrder(Order order) {
        return dao.editOrder(order);
    }

    @Override
    @Transactional
    public Order editOrderStatus(Order order) {
        Order order1 = dao.getOrderByID(order.getId());
        if (order1.getStatus().equals("待处理")){
            order1.setStatus("运输中");
        }
        else if (order1.getStatus().equals("已送达")){
            order1.setStatus("已签收");
        }
        dao.editOrder(order1);
        return order1;
    }

    @Override
    public Order getOrderByID(int id) {
        return dao.getOrderByID(id);
    }

    @Override
    public List<Order> getOrder(Order order, Page page) {
        return dao.getOrder(order,page);
    }

    @Override
    public List<Order> getOrderByStationCode(Order order) {
        return dao.getOrderByStationCode(order);
    }

    @Override
    public int getCount(Order order) {
        return dao.getCount(order);
    }

    @Override
    @Transactional
    public LogisticsOrderStatistics getStatistics() {
        // 调用mapper方法获取每个统计数据
        int total = logisticsOrderMapper.total();
        int smallCount = logisticsOrderMapper.smallCount();
        int mediumCount = logisticsOrderMapper.mediumCount();
        int largeCount = logisticsOrderMapper.largeCount();
        int chuLiCount = logisticsOrderMapper.chuLiCount();
        int yunShuCount = logisticsOrderMapper.yunShuCount();
        int songDaCount = logisticsOrderMapper.songDaCount();
        int qianShouCount = logisticsOrderMapper.qianShouCount();

        LogisticsOrderStatistics statistics = new LogisticsOrderStatistics();
        statistics.setTotal(total);
        statistics.setSmallCount(smallCount);
        statistics.setMediumCount(mediumCount);
        statistics.setLargeCount(largeCount);
        statistics.setChuLiCount(chuLiCount);
        statistics.setYunShuCount(yunShuCount);
        statistics.setSongDaCount(songDaCount);
        statistics.setQianShouCount(qianShouCount);

        return statistics;
    }
}
